package com.mach.platform.api.controller;


import com.mach.platform.api.domain.Bill;
import com.mach.platform.api.service.BillServiceImpl;
import com.mach.platform.controller.BaseCtrl;
import com.mach.platform.domain.Response;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 小程序账单相关的接口
 */
@RequestMapping("/api/bill")
@RestController
public class BillCtrl extends BaseCtrl<Bill> {

    @Autowired
    private BillServiceImpl billService;


    /**
     *
     * @param id 账单的id
     * @return
     */
    @ApiOperation(value = "查询账单明细")
    @PostMapping("info")
    @Override
    public Response info(Long id) {
        Bill bill = billService.info(id);
        if (bill != null){
            return Response.ok(bill);
        }
        return Response.error("无记录");
    }

    @Override
    public Response list(Pageable pageable, Map params) {
        return null;
    }

    /**
     *
     * @param ids id的集合
     * @return
     */
    @ApiOperation(value = "删除账单", produces = "application/json")
    @PostMapping("del")
    @Override
    public Response delete(@RequestBody Long[] ids) {
        int result = billService.delete(ids);
        if (result > 0) {
            return Response.ok("删除成功");
        }
        return Response.error("删除失败");
    }

    /**
     *
     * @param bill
     * @return
     */
    @ApiOperation(value = "新增账单", produces = "application/json")
    @PostMapping("add")
    @Override
    public Response save(@RequestBody Bill bill) {
        Bill result = billService.save(bill);
        if (result != null) {
            return Response.ok(result);
        }
        return Response.error("新增失败");
    }

    /**
     *
     * @param bill
     * @return
     */
    @ApiOperation(value = "修改账单", produces = "application/json")
    @PostMapping("update")
    @Override
    public Response update(@RequestBody Bill bill) {
        Bill result = billService.update(bill);
        if (result != null) {
            return Response.ok(result);
        }
        return Response.error("修改失败");
    }
}
